package com.example.multithread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionTest {
    public static String msg = null;

    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        try {
            lock.lock();
            new Thread(() -> {
                lock.lock();
                try {
                    System.out.printf("【%s】准备通行数据的处理操作", Thread.currentThread().getName());
                    TimeUnit.SECONDS.sleep(2);
                    msg = "杨德军";
                    condition.signal();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }, "子线程").start();
            condition.await();
            System.out.println("【主线程】得到最终处理的结果: " + msg);
        } catch (Exception e) {

        }
    }
}
